package com.freestyle.seatabusiness.authorization.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by rocklee on 2022/4/1 14:46
 */
@Configuration
public class CacheConfig {
  @Bean
  public CaffeineCacheManager caffeineCacheManager(@Value("${spring.cache.cache-names:default}")List cacheNames,
                                                   @Value("${spring.cache.caffeine.spec:}")String spec){
    CaffeineCacheManager cacheManager = new CaffeineCacheManager();
    if (!CollectionUtils.isEmpty(cacheNames)) {
      cacheManager.setCacheNames(cacheNames);
    }
    if (StringUtils.hasText(spec)) {
      cacheManager.setCacheSpecification(spec);
    }
    return cacheManager;
  }
  @Bean
  public Cache cache(CacheManager cacheManager){
    return cacheManager.getCache("default");
  }
}
